package ylzy.com.yanglezhiyang;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import java.util.ArrayList;
import java.util.Random;

public class LevelUtil {

    private static final String TAG = "LevelUtil";
    public static ArrayList<Bitmap> bmps = new ArrayList<>();
    public static int width;
    public static int height;
    public static int spiritWidth;
    public static int spiritHeight;
    public static float scale;
    public static int actionBarHeight;
    public static int statusBarHeight;
    public static Bitmap gameObjectBg;//纹理


    public static void init(Context context){
        if(bmps==null || bmps.size()==0){

            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_0));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_1));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_2));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_3));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_4));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_5));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_6));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_7));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_8));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_9));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_10));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_11));
            bmps.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.spite_12));
        }

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        width = dm.widthPixels;
        height= dm.heightPixels;
        scale = context.getResources().getDisplayMetrics().density;

        TypedValue tv =new TypedValue();

        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }

        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        height = height - actionBarHeight - statusBarHeight;

        spiritWidth = width/9;
        spiritHeight = spiritWidth;

        gameObjectBg = BitmapFactory.decodeResource(context.getResources(), R.mipmap.bg);

    }

    public static ArrayList<Bitmap> getBmps(){
        return bmps;
    }

    public static ArrayList<Floor> getLevel1(){
        ArrayList<Floor> floors = new ArrayList<>();

        Floor floor = new Floor(9);
        floor.setMoveDownHalf(false);
        floor.addSpirits(getLevelFloor(true));
        floors.add(floor);

        floor = new Floor(9);
        floor.setMoveDownHalf(true);
        floor.addSpirits(getLevelFloor(false));
        floors.add(floor);

        return floors;
    }

    public static Point[] getLayout(boolean isMoveDownHalf){
        int row = 7;
        int line = 5;
        if(isMoveDownHalf){
            row = 6;
            line = 4;
        }

        Point[] ps = new Point[row*line];
        for(int i=0;i<line;i++){
            for(int j=0;j<row;j++){
                Point p = new Point();
                if(isMoveDownHalf){
                    p.x = j * spiritWidth + spiritWidth + spiritWidth/2;
                    p.y = i * spiritWidth + spiritWidth*3 + spiritWidth/2;
                }else{
                    p.x = j * spiritWidth + spiritWidth;
                    p.y = i * spiritWidth + spiritWidth*3;
                }

                ps[i*row + j] = p;
            }
        }

        return ps;
    }

    public static int getSpiritWidth() {
        return spiritWidth;
    }

    public static int getSpiritHeight() {
        return spiritHeight;
    }


    public static ArrayList<Floor> getLevel2(){
        ArrayList<Floor> floors = new ArrayList<>();
        Floor floor = null;
        for(int i=0;i<15;i++){
            floor = new Floor(20);
            floor.setMoveDownHalf(i%2==0);
            floors.add(floor);
        }

        floor = new Floor(5);
        floor.setStack(true);
        floor.setXY(spiritWidth*3, spiritHeight*10);
        floor.setStackDirection(Floor.StackDirection.UP);
        floors.add(floor);

        floor = new Floor(5);
        floor.setStack(true);
        floor.setXY(spiritWidth*5, spiritHeight*10);
        floor.setStackDirection(Floor.StackDirection.UP);
        floors.add(floor);

        floor = new Floor(16);
        floor.setStack(true);
        floor.setXY(spiritWidth*2, spiritHeight*11);
        floor.setStackDirection(Floor.StackDirection.LEFT);
        floors.add(floor);

        floor = new Floor(16);
        floor.setStack(true);
        floor.setXY(spiritWidth*6, spiritHeight*11);
        floor.setStackDirection(Floor.StackDirection.RIGHT);
        floors.add(floor);

        Random random = new Random();
        for(int i=0;i<80;i++){
            int c = Math.abs(random.nextInt()%getBmps().size());
            for(int j=0;j<3;j++){
                GameObject gameObject = new GameObject(c, getBmps().get(c));
                while (true){
                    int r = Math.abs(random.nextInt()%floors.size());
                    if(floors.get(r).checkCanAdd()){
                        gameObject.setFloor(floors.get(r));
                        floors.get(r).addSpirit(gameObject);
                        break;
                    }
                }
            }
        }

        for(Floor f : floors){
           f.layout();
        }

        return floors;

    }

    public static ArrayList<GameObject> getLevelFloor(boolean isMoveDownHalf){
        ArrayList<GameObject> spirits = new ArrayList<>();
        GameObject gameObject = null;

        if(isMoveDownHalf){
            gameObject = new GameObject(spiritWidth*3-spiritWidth/2,spiritWidth*3,spiritWidth,spiritWidth,0, getBmps().get(0), 0);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*4,spiritWidth*3,spiritWidth,spiritWidth,0, getBmps().get(1), 0);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*5 + spiritWidth/2,spiritWidth*3,spiritWidth,spiritWidth,0, getBmps().get(2), 0);
            spirits.add(gameObject);

            gameObject = new GameObject(spiritWidth*3-spiritWidth/2,spiritWidth*5,spiritWidth,spiritWidth,0, getBmps().get(0), 0);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*4,spiritWidth*5,spiritWidth,spiritWidth,0, getBmps().get(1), 0);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*5 + spiritWidth/2,spiritWidth*5,spiritWidth,spiritWidth,0, getBmps().get(2), 0);
            spirits.add(gameObject);

            gameObject = new GameObject(spiritWidth*3-spiritWidth/2,spiritWidth*7,spiritWidth,spiritWidth,0, getBmps().get(0), 0);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*4,spiritWidth*7,spiritWidth,spiritWidth,0, getBmps().get(1), 0);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*5 + spiritWidth/2,spiritWidth*7,spiritWidth,spiritWidth,0, getBmps().get(2), 0);
            spirits.add(gameObject);


        }else{
            gameObject = new GameObject(spiritWidth*3-spiritWidth/2,spiritWidth*3 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(0), 1);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*4,spiritWidth*3 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(1), 1);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*5 + spiritWidth/2,spiritWidth*3 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(2), 1);
            spirits.add(gameObject);

            gameObject = new GameObject(spiritWidth*3-spiritWidth/2,spiritWidth*5 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(0), 1);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*4,spiritWidth*5 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(1), 1);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*5 + spiritWidth/2,spiritWidth*5 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(2), 1);
            spirits.add(gameObject);

            gameObject = new GameObject(spiritWidth*3-spiritWidth/2,spiritWidth*7 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(0), 1);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*4,spiritWidth*7 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(1), 1);
            spirits.add(gameObject);
            gameObject = new GameObject(spiritWidth*5 + spiritWidth/2,spiritWidth*7 + spiritWidth/2,spiritWidth,spiritWidth,0, getBmps().get(2), 1);
            spirits.add(gameObject);


        }


        return spirits;
    }




}
